package com.itheima.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.OperationalDataMapper;
import com.itheima.service.OperationalDataService;
import com.itheima.utils.DateUtil;
import com.itheima.vo.OperationalData;
import org.apache.dubbo.config.annotation.Service;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@Service
public class OperationalDataServiceImpl  extends ServiceImpl<OperationalDataMapper,OperationalData> implements OperationalDataService{


    @Override
    public Map<String, Object> reportData() {

        //获取当前日
        String today = DateUtil.formatDate(new Date());
        //获取当前周
        String weekNum = String.valueOf(DateUtil.getWeekNum(new Date()));
        //获取本月
        String mouthNum = DateUtil.formatDate(new Date(), "MM");

        long start = System.currentTimeMillis();


        ExecutorService executorService = Executors.newFixedThreadPool(12);

        Future<Integer> todayNewMember = executorService.submit(() -> {
            return this.baseMapper.findTodayNewMember(today);
        });

        Future<Integer> totalMember =  executorService.submit(()->{
            //总会员数
            return this.baseMapper.findTodayMember();
        });

        Future<Integer> thisWeekNewMember =  executorService.submit(()->{
            //本周新增会员数
            return this.baseMapper.findThisWeekNewMember(weekNum);
        });

        Future<Integer>  thisMonthNewMember =  executorService.submit(()->{
            //本月新增会员数
            return this.baseMapper.findThisMonthNewMember(mouthNum);
        });

        Future<Integer> todayOrderNumber =  executorService.submit(()->{
            //今日预约数
            return this.baseMapper.findTodayOrderNumber(today);
        });

        Future<Integer> todayVisitsNumber =  executorService.submit(()->{
            //今日到诊数
            return this.baseMapper.findTodayVisitsNumber(today);
        });

        Future<Integer> thisWeekOrderNumber =  executorService.submit(()->{
            //本周预约数
            return this.baseMapper.findThisWeekOrderNumber(weekNum);
        });

        Future<Integer> thisWeekVisitsNumber =  executorService.submit(()->{
            //本周到诊数
            return this.baseMapper.findThisWeekVisitsNumber(weekNum);
        });

        Future<Integer> thisMonthOrderNumber =  executorService.submit(()->{
            //本月预约数
            return this.baseMapper.findThisMonthOrderNumber(mouthNum);
        });

        Future<Integer> thisMonthVisitsNumber =  executorService.submit(()->{
            //本月到诊数
            return this.baseMapper.findThisMonthVisitsNumber(mouthNum);
        });

        Future<List<Map>> hotSetmeal =  executorService.submit(()->{
            //热门套餐
            return this.baseMapper.findHotSetmeal();
        });


        long end = System.currentTimeMillis();

        System.out.println("SQL查询耗时: " + (end - start));

        Map<String, Object> data = new HashMap<>();


        try {
            //日期
            data.put("reportDate",today);
            data.put("todayNewMember",todayNewMember.get());
            data.put("totalMember",totalMember.get());
            data.put("thisWeekNewMember",thisWeekNewMember.get());
            data.put("thisMonthNewMember",thisMonthNewMember.get());
            data.put("todayOrderNumber",todayOrderNumber.get());
            data.put("todayVisitsNumber",todayVisitsNumber.get());
            data.put("thisWeekOrderNumber",thisWeekOrderNumber.get());
            data.put("thisWeekVisitsNumber",thisWeekVisitsNumber.get());
            data.put("thisMonthOrderNumber",thisMonthOrderNumber.get());
            data.put("thisMonthVisitsNumber",thisMonthVisitsNumber.get());
            data.put("hotSetmeal",hotSetmeal.get());

        }catch (Exception e){
            e.printStackTrace();
        }


        return data;


    }

}
